﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Liang.Device.Camera.Action;
using Liang.Device.Model.Camera;

namespace Liang.Device.Camera
{
    /// <summary>
    /// 摄像头辅助工具类
    /// </summary>
    /// <example>
    /// <code>
    /// AipStar camera = new AipStar();
    /// // 打开摄像头
    /// CameraHelper.Open(camera);
    /// // 播放视频
    /// CameraHelper.Play(camera);
    /// // 抓拍
    /// CameraHelper.Capture(camera, @"c:\123.jpg");
    /// // 停止播放
    /// CameraHelper.Stop(camera);
    /// // 关闭摄像头
    /// CameraHelper.Close(camera);
    /// </code>
    /// </example>
    public static class CameraHelper
    {
        /// <summary>
        /// 打开参数指定的摄像头
        /// </summary>
        /// <param name="obj">摄像头参数</param>
        /// <returns></returns>
        public static bool Open(object obj)
        {
            if (null != obj)
            {
                Type t = obj.GetType();

                if (t == typeof(Wision) || t.BaseType == typeof(Wision))
                {
                    return WisionCamera.Instance.Open((Wision)obj);
                }

                if (t == typeof(AipStar) || t.BaseType == typeof(AipStar))
                {
                    return AipStarCamera.Instance.Open((AipStar)obj);
                }

                if (t == typeof(Camyu) || t.BaseType == typeof(Camyu))
                {
                    return CamyuCamera.Instance.Open((Camyu)obj);
                }

                if (t == typeof(DaHua) || t.BaseType == typeof(DaHua))
                {
                    return DaHuaCamera.Instance.Open((DaHua)obj);
                }

                if (t == typeof(DaHuaVMS) || t.BaseType == typeof(DaHuaVMS))
                {
                    return DaHuaVMSCamera.Instance.Open((DaHuaVMS)obj);
                }

                if (t == typeof(HikVision) || t.BaseType == typeof(HikVision))
                {
                    return HikVisionCamera.Instance.Open((HikVision)obj);
                }
            }

            return false;
        }

        /// <summary>
        /// 关闭参数指定的摄像头
        /// </summary>
        /// <param name="obj">摄像头参数</param>
        /// <returns></returns>
        public static bool Close(object obj)
        {
            if (null != obj)
            {
                Type t = obj.GetType();

                if (t == typeof(Wision) || t.BaseType == typeof(Wision))
                {
                    return WisionCamera.Instance.Close((Wision)obj);
                }

                if (t == typeof(AipStar) || t.BaseType == typeof(AipStar))
                {
                    return AipStarCamera.Instance.Close((AipStar)obj);
                }

                if (t == typeof(Camyu) || t.BaseType == typeof(Camyu))
                {
                    return CamyuCamera.Instance.Close((Camyu)obj);
                }

                if (t == typeof(DaHua) || t.BaseType == typeof(DaHua))
                {
                    return DaHuaCamera.Instance.Close((DaHua)obj);
                }

                if (t == typeof(DaHuaVMS) || t.BaseType == typeof(DaHuaVMS))
                {
                    return DaHuaVMSCamera.Instance.Close((DaHuaVMS)obj);
                }

                if (t == typeof(HikVision) || t.BaseType == typeof(HikVision))
                {
                    return HikVisionCamera.Instance.Close((HikVision)obj);
                }
            }

            return false;
        }

        /// <summary>
        /// 播放参数指定的摄像头视频
        /// </summary>
        /// <param name="obj">摄像头参数</param>
        /// <returns></returns>
        public static bool Play(object obj)
        {
            if (null != obj)
            {
                Type t = obj.GetType();

                if (t == typeof(Wision) || t.BaseType == typeof(Wision))
                {
                    return WisionCamera.Instance.Play((Wision)obj);
                }

                if (t == typeof(AipStar) || t.BaseType == typeof(AipStar))
                {
                    return AipStarCamera.Instance.Play((AipStar)obj);
                }

                if (t == typeof(Camyu) || t.BaseType == typeof(Camyu))
                {
                    return CamyuCamera.Instance.Play((Camyu)obj);
                }

                if (t == typeof(DaHua) || t.BaseType == typeof(DaHua))
                {
                    return DaHuaCamera.Instance.Play((DaHua)obj);
                }

                if (t == typeof(DaHuaVMS) || t.BaseType == typeof(DaHuaVMS))
                {
                    return DaHuaVMSCamera.Instance.Play((DaHuaVMS)obj);
                }

                if (t == typeof(HikVision) || t.BaseType == typeof(HikVision))
                {
                    return HikVisionCamera.Instance.Play((HikVision)obj);
                }
            }

            return false;
        }

        /// <summary>
        /// 停止播放参数指定的摄像头
        /// </summary>
        /// <param name="obj">摄像头参数</param>
        /// <returns></returns>
        public static bool Stop(object obj)
        {
            if (null != obj)
            {
                Type t = obj.GetType();

                if (t == typeof(Wision) || t.BaseType == typeof(Wision))
                {
                    return WisionCamera.Instance.Stop((Wision)obj);
                }

                if (t == typeof(AipStar) || t.BaseType == typeof(AipStar))
                {
                    return AipStarCamera.Instance.Stop((AipStar)obj);
                }

                if (t == typeof(Camyu) || t.BaseType == typeof(Camyu))
                {
                    return CamyuCamera.Instance.Stop((Camyu)obj);
                }

                if (t == typeof(DaHua) || t.BaseType == typeof(DaHua))
                {
                    return DaHuaCamera.Instance.Stop((DaHua)obj);
                }

                if (t == typeof(DaHuaVMS) || t.BaseType == typeof(DaHuaVMS))
                {
                    return DaHuaVMSCamera.Instance.Stop((DaHuaVMS)obj);
                }

                if (t == typeof(HikVision) || t.BaseType == typeof(HikVision))
                {
                    return HikVisionCamera.Instance.Stop((HikVision)obj);
                }
            }

            return false;
        }

        /// <summary>
        /// 抓拍图片
        /// </summary>
        /// <param name="obj">摄像头参数</param>
        /// <param name="strImageFile">图片文件</param>
        /// <returns></returns>
        public static bool Capture(object obj, string strImageFile)
        {
            if (null != obj)
            {
                Type t = obj.GetType();

                if (t == typeof(Wision) || t.BaseType == typeof(Wision))
                {
                    return WisionCamera.Instance.Capture((Wision)obj, strImageFile);
                }

                if (t == typeof(AipStar) || t.BaseType == typeof(AipStar))
                {
                    return AipStarCamera.Instance.Capture((AipStar)obj, strImageFile);
                }

                if (t == typeof(Camyu) || t.BaseType == typeof(Camyu))
                {
                    return CamyuCamera.Instance.Capture((Camyu)obj, strImageFile);
                }

                if (t == typeof(DaHua) || t.BaseType == typeof(DaHua))
                {
                    return DaHuaCamera.Instance.Capture((DaHua)obj, strImageFile);
                }

                if (t == typeof(DaHuaVMS) || t.BaseType == typeof(DaHuaVMS))
                {
                    return DaHuaVMSCamera.Instance.Capture((DaHuaVMS)obj, strImageFile);
                }

                if (t == typeof(HikVision) || t.BaseType == typeof(HikVision))
                {
                    return HikVisionCamera.Instance.Capture((HikVision)obj, strImageFile);
                }
            }

            return false;
        }
    }
}
